﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;

namespace CsharpTrainer.NewJson.Linq
{
    public class LinqToJson
    {
        public static void QueryTest()
        {

            string json = @"{
                          ""hotel"": [
                                {
                                  ""name"": ""新锦江"",
                                  ""rank"": ""4"",
                                },
                                {
                                  ""name"": ""希尔顿"",
                                  ""rank"": ""5"",
                                }
                            ]
                      }";

            JObject o = JObject.Parse(json);

            Console.Write("Json Content: ");
            Console.WriteLine(json);
            Console.Write("Name Count: ");
            Console.WriteLine(o["hotel"].Children()["name"].Count());
            Console.Write("Rank Count: ");
            Console.WriteLine(o["hotel"].Children()["rank"].Count());

            string[] ranks = o["hotel"].Values<string>("rank").ToArray();
            for (int i = 0; i < ranks.Length; i++)
                Console.WriteLine(ranks[i]);
        }
    }
}


